package com.cqc.manage.controller;

import com.cqc.manage.dto.JsonResultDto;
import com.cqc.manage.dto.PayloadDto;
import com.cqc.manage.entity.User;
import com.cqc.manage.util.JdbcTemplateSql;
import com.cqc.manage.service.Imp.user.UserService;
import com.cqc.manage.token.JwtTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 标签控制类
 */
@RestController
@RequestMapping("/label")
@Api(tags = "label" , description = "角色相关的操作")
public class LabelController {
    private static Logger LOG = LoggerFactory.getLogger(LabelController.class);

    @Autowired
    private JdbcTemplateSql labelJdbcTemplate;
    @Autowired
    private JwtTokenService jwtTokenService;
    @Autowired
    private UserService userService;

    /**
     * 管理员获取全部权限
     */
    @ApiOperation(value = "获取所有label",notes = "获取所有label")
    @RequestMapping(value = "/labelOne",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getAllLebel(HttpServletRequest request) throws Exception{
        String authHeader = request.getHeader("Authorization");
        String token = authHeader.substring(7);
        // 验证token
        PayloadDto test = jwtTokenService.verifyTokenByHMAC(token);

        User byUserAccount = userService.findByUserAccount(test.getUserName());
        List<Map<String, Object>> userLabel = labelJdbcTemplate.getUserLabel(byUserAccount);

//        List<Map<String, Object>> allLabel = labelJdbcTemplate.getAllLabel();
        LOG.info("全部权限:{}",userLabel);
        return new JsonResultDto<List<Map<String, Object>>>("标签获取成功","200",userLabel);
    }
}
